﻿using Coldairarrow.Entity.Base_Manage;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_ShiftTypeBusiness : BaseBusiness<PB_ShiftType>, IPB_ShiftTypeBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public PB_ShiftTypeBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<PageResult<PB_ShiftType>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ShiftType>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_ShiftType, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }
           
            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_ShiftType> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_ShiftType data)
        {
            var quchong = Db.GetIQueryable<PB_ShiftType>().Where(m=>m.WorkShopID==data.WorkShopID&&m.Name==data.Name&&data.Deleted==false).FirstOrDefault();
            var Chejian = Db.GetIQueryable<Base_Department>().Where(m => m.Id == data.WorkShopID).FirstOrDefault();
            if (quchong!=null) {
                throw new Exception($"{Chejian.Name}已经存在班别{quchong.Name}");
            }
            data.Deleted = false;
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_ShiftType data)
        {
            var quchong = Db.GetIQueryable<PB_ShiftType>().Where(m => m.WorkShopID == data.WorkShopID && m.Name == data.Name && data.Deleted == false).FirstOrDefault();
            var Chejian = Db.GetIQueryable<Base_Department>().Where(m => m.Id == data.WorkShopID).FirstOrDefault();
            if (quchong != null)
            {
                throw new Exception($"{Chejian.Name}已经存在班别{quchong.Name}");
            }
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public async Task<PageResult<PB_ShiftType>> GetDataListAsyncworkid(PageInput<ConditionDTO> input, string workid)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ShiftType>();
            var search = input.Search;

     
            q = q.Where(m=>m.WorkShopID==workid);
            q.GroupBy(m=>m.Code);
            return await q.Where(where).GetPageResultAsync(input);
        }

        public  async  Task<PageResult<PB_ShiftType>> GetDataListAsyncworkidBai(PageInput<ConditionDTO> input, string workid)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ShiftType>();
            var search = input.Search;
            q = q.Where(m => m.WorkShopID == workid);
            q = q.Where(m=>m.Name=="白");
            return await q.Where(where).GetPageResultAsync(input);
        }


        public async Task<PageResult<PB_ShiftType>> GetDataListAsyncworkidYe(PageInput<ConditionDTO> input, string workid)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_ShiftType>();
            var search = input.Search;
            q = q.Where(m => m.WorkShopID == workid);
            q = q.Where(m => m.Name == "夜");
            return await q.Where(where).GetPageResultAsync(input);
        }

        public  async Task<PageResult<PB_ShiftTypeDTO>> GetDataListAsyncDTO(PageInput<ConditionDTO> input)
        {
            var search = input.Search;
            Expression<Func<PB_ShiftType, Base_Department, PB_ShiftTypeDTO>> select = (a, b) => new PB_ShiftTypeDTO
            {
                 WorkName= b.Name,
                 deptid=b.Id,
            };
            select = select.BuildExtendSelectExpre();
            var q = from a in GetIQueryable().AsExpandable()
                    join b in Db.GetIQueryable<Base_Department>() on a.WorkShopID equals b.Id into ab
                    from b in ab.DefaultIfEmpty()
                    select @select.Invoke(a, b);
            //筛选
            if (!search.Keyword.IsNullOrEmpty())
            {
                //var keyword = $"%{search.Keyword.Trim()}%";
                var keyword = search.Keyword.Trim();
                //修改多条件匹配
                //q = q.Where(x => EF.Functions.Like(search.Condition, keyword) );
                //q = q.Where(x => EF.Functions.Like("WorkName", keyword)|| EF.Functions.Like("Name",keyword) );
                q = q.Where(x=>x.WorkName.Contains(keyword)||x.Name.Contains(keyword));
            }
           q= q.Where(h => _operator.Property.DeptIds.Contains(h.deptid));
            return await q.GetPageResultAsync(input);
        }

        /// <summary>
        /// 获取当前车间下的班别
        /// </summary>
        /// <param name="WorkShopId"></param>
        /// <returns></returns>
        public async Task<List<PB_ShiftType>> GetWorkShiftType(string WorkShopId)
        {
            var list =await Db.GetIQueryable<PB_ShiftType>().Where(m=>m.WorkShopID==WorkShopId).ToListAsync();
            return list;
        }

        #endregion

        #region 私有成员

        #endregion
    }
}